<?php

/**
 * Delivery Return Head
 */
class DeRtH extends AppModel {

    var $name = 'DeRtH';
    var $useTable = 'delivery_return_heads';
    var $belongsTo = array(
        'Supplier', 'Customer', 'Site',
        'DeRH' => array(
            'foreignKey' => 'delivery_request_head_id'
        ),
        'DeRtStatus' => array(
            'foreignKey' => 'delivery_return_status_id'
        ),
        'User' => array(
            'foreignKey' => 'collector_id'
        ),
    );
    var $hasMany = array(
        'DeRt' => array(
            'dependent' => true
        )
    );
    var $validate = array(
        'return_no' => array(
            'rule' => array('notEmpty'),
            'message' => 'Request no. is required.',
            'required' => true,
            'on' => 'create'
        ),
        'return_date' => array(
            'rule' => array('notEmpty'),
            'message' => 'Request date is required.',
            'required' => true,
            'on' => 'create'
        ),
        'return_by' => array(
            'rule' => 'notEmpty',
            'message' => 'Request by is required.',
            'required' => true,
            'on' => 'create'
        ),
        'supplier_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Supplier is required.',
            'required' => true,
            'on' => 'create'
        ),
        'customer_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Customer is required.',
            'required' => true,
            'on' => 'create'
        ),
        'site_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Site is required.',
            'required' => true,
            'on' => 'create'
        ),
        'delivery_return_status_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Status is required.',
            'required' => true,
            'on' => 'create'
        ),
    );

    function beforeSave() {
        if (isset($this->data['DeRtH']['return_date'])) {
            $this->data['DeRtH']['return_date'] = $this->dateFormatBeforeSave($this->data['DeRtH']['return_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$DeRtH) {
            if (isset($DeRtH['DeRtH'])) {
                if (isset($DeRtH['DeRtH']['return_date'])) {
                    $DeRtH['DeRtH']['return_date'] = $this->dateFormatAfterFind($DeRtH['DeRtH']['return_date']);
                }
            }
        }
        return $results;
    }

}

?>
